<extend  name="Public:base" />
<block name="content_header">
    <div class="content-header">
        <h2 class="content-title">权限管理</h2>
        <span class="layui-breadcrumb">
            <a href="{:U('Index/index')}">首页</a>
            <a>系统设置</a>
            <a><cite>权限管理</cite></a>
        </span>
    </div>
</block>
<block name="content_body">
    <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
        <ul class="layui-tab-title">
            <li class="layui-this" id="li1">页面注册</li>
            <li id="li2">角色管理</li>
            <li id="li3">系统管理员管理</li>
        </ul>
        <div class="layui-tab-content" id="div1">
            <div class="layui-tab-item layui-show" style="width:600px;">
                <form class="layui-form" action="">
                    <div class="layui-form-item">
                        <label class="layui-form-label">页面标题</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" required  lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">链接地址</label>
                        <div class="layui-input-block">
                            <input type="text" name="auth" placeholder="请输入链接地址，全名，例如Admin/Index/index" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">一级目录</label>
                        <div class="layui-input-block">
                            <select name="pid" lay-filter="menu">
                                <option value=""></option>
                                <option value="0">根目录</option>
                                <volist name="topmenu" id="t">
                                    <option value="{$t.id}">{$t.name}</option>
                                </volist>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">二级目录</label>
                        <div class="layui-input-block">
                            <select name="child" class="child" lay-filter="child">
                                <option></option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">所属权限</label>
                        <div class="layui-input-block">
                            <volist name="auth_group" id="au">
                                <input type="checkbox" name="au[{$au.id}]" title="{$au.name}">
                            </volist>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">排序</label>
                        <div class="layui-input-block" >
                            <input type="text" name="sort" placeholder="" style="width: 150px"  autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-btn" lay-submit lay-filter="submit">注册</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                </form>
            </div>
            <div class="layui-tab-item" style="height: 500px;" id="div2">
                <div class="layui-btn-group toolbar">
                    <button class="layui-btn" data-type="add">新增</button>
                </div>
                <table id="dataTable" lay-filter="role"></table>
                <script type="text/html" id="handle">
                    <a class="layui-btn layui-btn-xs" lay-event="edit">详情</a>
                    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                </script>
            </div>
            <div class="layui-tab-item" style="height: 500px;" id="div3">
                <div class="layui-inline">
                    用户名：<input class="layui-input search-input" type="text" name="username" id="username" placeholder="请输入关键字" />
                </div>
                <div class="layui-btn-group toolbar">
                    <button class="layui-btn" data-type="search">搜索</button>
                </div>
                <div class="layui-btn-group toolbar">
                    <button class="layui-btn" data-type="enable">启用</button>
                    <button class="layui-btn layui-btn-warm" data-type="disable">禁用</button>
                    <button class="layui-btn layui-btn-danger" data-type="delmem">删除</button>
                </div>
                <div class="layui-btn-group toolbar">
                    <button class="layui-btn" data-type="addmem">新增</button>
                </div>
                <table id="dataMember" lay-filter="member"></table>
                <script type="text/html" id="memberhandle">
                    <a class="layui-btn layui-btn-xs" lay-event="editmem">编辑用户</a>
                    <a class="layui-btn layui-btn-xs" lay-event="resetpassword">重置管理员密码</a>
                    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delmem">删除</a>
                </script>
                <script type="text/html" id="changeStatusBtn">
                    <input type="checkbox" name="changeStatus" value="{{d.id}}" lay-skin="switch" lay-text="正常|禁用" lay-filter="memchangeStatus" {{ d.status == 1 ? 'checked' : '' }}>
                </script>
            </div>
        </div>
    </div>
</block>
<block name="foot_script">
    <script>
        $(layui.use(['layer','form','table','upload'], function(){
            var $ = layui.$, layer = layui.layer,form = layui.form,table = layui.table,upload = layui.upload;

            form.on('submit(submit)',function (data) {
                $.post('{:U("Adminrole/addPageRole")}',data.field,function (res) {
                    if (res.code === 2){
                        layer.msg(res.msg,{time:1000},function () {
                            location.reload();
                        });
                    } else if(res.code === 1){
                        layer.msg(res.msg,{time:1000},function () {
                            window.location.href=res.url;
                        });
                    }else{
                        layer.msg(res.msg,{time:1000});
                    }
                });
                return false;
            });
            form.on('select(menu)', function(data){
                var url = "{:U('Adminrole/get_second')}"
                $.post(url,{id:data.value},function (res) {
                    if (res.code === 1){
                        var data = res.data;
                        var str = "<option></option>";
                        for( var key in data)
                        {
                            str +='<option value="'+data[key]['id']+'">'+ data[key]['name']+ '</option>'
                        }

                        $('.child').children().replaceWith(str)
                        form.render('select');
                    }
                });
            });
            $('.toolbar .layui-btn').on('click', function(){
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
            var active = {
                search: function(){
                    var username = $('#username');
                    //执行重载
                    table.reload('dataMember', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                        ,where: {
                            username: username.val(),
                        }
                    });
                }
                ,enable: function(){
                    //启用
                    var url = '{:U("Adminrole/memchangeStatus")}';
                    var checkStatus = table.checkStatus('dataMember')
                        ,data = checkStatus.data;
                    if (data.length <= 0){
                        layer.msg("未选择");
                        return;
                    }
                    // 组装ID
                    var ids = [];
                    for (var i in data){
                        ids.push(data[i].id);
                    }
                    $.post(url,{ids:ids,method:'enable'},function (res) {
                        if (res.code === 1){
                            layer.msg(res.msg);
                            table.reload('dataMember');
                        } else {
                            layer.msg(res.msg);
                        }
                    });
                }
                ,disable: function(){
                    //禁用
                    var url = '{:U("Adminrole/memchangeStatus")}';
                    var checkStatus = table.checkStatus('dataMember')
                        ,data = checkStatus.data;
                    if (data.length <= 0){
                        layer.msg("未选择");
                        return;
                    }
                    // 组装ID
                    var ids = [];
                    for (var i in data){
                        ids.push(data[i].id);
                    }
                    $.post(url,{ids:ids,method:'disable'},function (res) {
                        if (res.code === 1){
                            layer.msg(res.msg);
                            table.reload('dataMember');
                        } else {
                            layer.msg(res.msg);
                        }
                    });
                }
                ,delmem: function(){
                    //删除
                    var url = '{:U("Adminrole/memchangeStatus")}';
                    var checkStatus = table.checkStatus('dataMember')
                        ,data = checkStatus.data;
                    if (data.length <= 0){
                        layer.msg("未选择");
                        return;
                    }
                    layer.confirm('确定删除吗?', {icon: 3, title:'提示'}, function(index){
                        // 组装ID
                        var ids = [];
                        for (var i in data){
                            ids.push(data[i].id);
                        }
                        $.post(url,{ids:ids,method:'del'},function (res) {
                            if (res.code === 1){
                                layer.msg(res.msg);
                                layer.close(index);
                                table.reload('dataMember');
                            } else {
                                layer.msg(res.msg);
                            }
                        });
                    });
                }
                ,addmem: function () {
                    //新增
                    openIframe('新增管理员','{:U("Adminrole/addmem")}',['600px','400px']);
                }
                ,add: function () {
                    //新增
                    openIframe('新增权限标题','{:U("Adminrole/add")}',['1200px','700px']);
                }
            };

        }));

        $('#li2').click(function(){
            layui.use('table', function(){
                var table = layui.table;
                // 渲染
                table.render({
                    elem: '#dataTable'
                    ,width:900
                    ,height: 'full-300'
                    ,url: '{:U("Adminrole/manage")}' //数据接口
                    ,method:'post'
                    ,page: true //开启分页
                    ,cols: [[ //表头
                        {field: 'id', title: 'ID', width:80, sort: true, fixed: 'left'}
                        ,{field: 'name', title: '权限名称', width:400}
                        ,{fixed: 'right',width:200, align:'center', toolbar: '#handle',title: '操作'}
                    ]]
                });

                // 监听工具条
                table.on('tool(role)', function(obj){
                    var data = obj.data;
                    if(obj.event === 'edit'){
                        var url = '{:U("Adminrole/edit")}';
                        if (url.indexOf('?') > 0){
                            url = url + '&id=' + data.id;
                        } else {
                            url = url + '?id=' + data.id;
                        }
                        openIframe('编辑用户密码',url,['1200px','700px']);
                    } else if(obj.event === 'del'){
                        var url = '{:U("Adminrole/changeStatus")}';
                        layer.confirm('真的删除吗？', function(index){
                            // 组装ID
                            var ids = [data.id];
                            $.post(url,{ids:ids,method:'del'},function (res) {
                                if (res.code === 1){
                                    layer.msg(res.msg);
                                    layer.close(index);
                                    table.reload('dataTable');
                                } else {
                                    layer.msg(res.msg);
                                }
                            });
                        });
                    }
            });
            });
        });
        $('#li3').click(function(){
            layui.use(['layer','form','table'], function(){
                var table = layui.table;var layer = layui.layer;var form = layui.form;

                //第一个实例
                table.render({
                    elem: '#dataMember'
                    ,height: 'full-300'
                    ,url: '{:U("Adminrole/member")}' //数据接口
                    ,method:'post'
                    ,page: true //开启分页
                    ,cols: [[ //表头
                        {type:'checkbox', fixed: 'left'}
                        ,{field: 'mid', title: 'ID', width:80, sort: true, fixed: 'left'}
                        ,{field: 'username', title: '用户名', width:200}
                        ,{field: 'name', title: '所属权限', width:200}
                        ,{field: 'status', title: '状态', width:150,toolbar: '#changeStatusBtn'}
                        ,{fixed: 'right',width:400, align:'center', toolbar: '#memberhandle',title: '操作'}
                    ]]
                });

                form.on('switch(memchangeStatus)', function(obj){
                    var ids = [this.value];
                    var url = '{:U("Adminrole/memchangeStatus")}';
                    if (obj.elem.checked){
                        // 启用
                        $.post(url,{ids:ids,method:'enable'},function (res) {
                            if (res.code === 1){
                                layer.msg(res.msg);
                                table.reload('dataMember');
                            } else {
                                layer.msg(res.msg);
                                table.reload('dataMember');
                            }
                        });
                    } else {
                        // 禁用
                        $.post(url,{ids:ids,method:'disable'},function (res) {
                            if (res.code === 1){
                                layer.msg(res.msg);
                            } else {
                                layer.msg(res.msg);
                                table.reload('dataMember');
                            }
                        });
                    }
                });

                //监听工具条
                table.on('tool(member)', function(obj){
                    var data = obj.data;
                    if(obj.event === 'editmem'){
                        var url = '{:U("Adminrole/memedit")}';
                        if (url.indexOf('?') > 0){
                            url = url + '&id=' + data.id;
                        } else {
                            url = url + '?id=' + data.id;
                        }
                        openIframe('编辑管理员权限',url,['600px','400px']);
                    } else if(obj.event === 'delmem'){
                        var url = '{:U("Adminrole/memchangeStatus")}';
                        layer.confirm('真的删除吗？', function(index){
                            // 组装ID
                            var ids = [data.id];
                            $.post(url,{ids:ids,method:'del'},function (res) {
                                if (res.code === 1){
                                    layer.msg(res.msg);
                                    layer.close(index);
                                    table.reload('dataMember');
                                } else {
                                    layer.msg(res.msg);
                                }
                            });
                        });
                    }else if(obj.event === 'resetpassword'){
                        var url = '{:U("Adminrole/resetpassword")}';
                        $.post(url,{id:data.id},function (res) {
                            if (res.code === 1){
                                layer.msg(res.msg);
                                table.reload('dataMember');
                            } else {
                                layer.msg(res.msg);
                            }
                        });
                    }
                });
            });
        });

    </script>

</block>